/*
 * convert.h
 *
 *  Created on: 28-nov-2008
 *      Author: k12938
 */

#ifndef CONVERT_H_
#define CONVERT_H_
 // File: convert.h
 #include <iostream>
 #include <sstream>
 #include <string>
 #include <stdexcept>

 class BadConversion : public std::runtime_error {
 public:
   BadConversion(const std::string& s)
     : std::runtime_error(s)
     { }
 };

 inline double convertToDouble(const std::string& s)
 {
   std::istringstream i(s);
   double x;
   if (!(i >> x))
     throw BadConversion("convertToDouble(\"" + s + "\")");
   return x;
 }

 inline int convertToInt(const std::string& s)
 {
   std::istringstream i(s);
   int x;
   if (!(i >> x))
     throw BadConversion("convertToInt(\"" + s + "\")");
   return x;
 }

 inline float convertToFloat(const std::string& s)
 {
   std::istringstream i(s);
   float x;
   if (!(i >> x))
     throw BadConversion("convertToFloat(\"" + s + "\")");
   return x;
 }

 inline long convertToLong(const std::string& s)
 {
   std::istringstream i(s);
   long x;
   if (!(i >> x))
     throw BadConversion("convertToLong(\"" + s + "\")");
   return x;
 }

#endif /* CONVERT_H_ */
